 
#include <string.h>
#include <math.h>
#include "system.h"
#include "f1c100s_de.h"
#include "f1c100s_timer.h"
#include "f1c100s_intc.h"
#include "f1c100s_gpio.h"
#include "f1c100s_pwm.h"
#include "f1c100s_uart.h"
#include "led8.h"
#include "arm32.h"
#include "string.h"
#define MAX_RX_BUF 64 
uint8_t rx_data[MAX_RX_BUF];
uint16_t re_count = 0;

void uart1_irq_handler(void)
{
    uint32_t it_status = uart_get_status(UART1);
    if (it_status & UART_LSR_DR)
    {
        rx_data[re_count] = uart_get_rx(UART1);
        re_count++;
        re_count = re_count % MAX_RX_BUF;
    }
}
void uart_irq_init()
{
    intc_enable_irq(IRQ_UART1);
    uart_enable_interrupt(UART1, UART_IEN_RBF);
    intc_set_irq_handler(IRQ_UART1, uart1_irq_handler);
}

void app_reset()
{
    intc_disable_irq(IRQ_UART1);
    intc_disable_irq(IRQ_TIMER0);
    arm32_interrupt_disable();
	wdg_init(WDG_MODE_RESET, WDG_INTV_500MS);
    while (1);
}

int main(void)
{
    uint16_t i=0,n=0;
	uint16_t last_count=0;
	system_init();
	gpio_init(GPIOA,PIN0,GPIO_MODE_OUTPUT,GPIO_PULL_NONE,GPIO_DRV_0);		//驱动PA0引脚点亮LED灯
	arm32_interrupt_enable(); // Enable interrupts
    uart_irq_init();
    printf("LED APP TIME:%s\r\n", __TIME__);
    while (1)
    {
        //port_a_output(0x00);
		if(++i==3000)
        {
            i=0;
            gpio_pin_toggle(GPIOA,0);			//PA0取反
			printf("The F1C200s N=%d\r\n",n++);
        }
        
        if(re_count)
        {
            if(re_count==last_count)
            {
                //printf("re_count=%d\r\n",re_count);
                if (memcmp(rx_data,"ResetNow",8)==0)			//从串口如果收到"ResetNow"，就重启芯片
                {
                    printf("Return TO BootLoad.\r\n");
					app_reset();
                }
                re_count=0;
            }
        }
        last_count=re_count;
		delay_soft(10);
    }
}

